//
//  AddBankCardTableViewController.m
//  HaoDeCaiFu
//
//  Created by 物联信息中心 on 2017/12/26.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "AddBankCardTableViewController.h"
#import "HDCFOpenAccountCell.h"

@interface AddBankCardTableViewController ()
@property (nonatomic, strong) NSArray *imageAry;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray *placeHoldArray;

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *idCardNum;
@property (nonatomic, copy) NSString *bankcode;
@property (nonatomic, copy) NSString *bankNumber;
@property (nonatomic, copy) NSString *telHp;
@property (nonatomic, copy) NSString *otp;
@property (nonatomic, copy) NSString *token;
@property (nonatomic, copy) NSString *tradeCode;
@property (nonatomic, weak) UIView *clearHudView;


@end

@implementation AddBankCardTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"添加银行卡";
    
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self configerFooterView];

    
    
}
-(UITableView*)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-49)];
        _tableView.backgroundColor=ZKcolor(240, 240, 240);
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
        
    }
    return _tableView;
}


- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [NSArray arrayWithObjects:@"姓名(必填)", @"身份证号(必填)", @"银行卡号(必填)", @"交易密码(必填)", @"银行预留手机号(必填)", @"短信验证码(必填)", nil];
    }
    return _titleArray;
}

- (NSArray *)imageAry{
    if (!_imageAry) {
        _imageAry = [NSArray arrayWithObjects:@"icon_xuanzeyinhang", @"icon_ka2",@"icon_yuliushouji", @"icon_yanzhengma", nil];
    }
    return _imageAry;
}
- (NSMutableArray *)placeHoldArray{
    if (!_placeHoldArray) {
        self.placeHoldArray = [NSMutableArray arrayWithObjects:@"请选择银行(必填)", @"请输入银行卡号(必填)", @"请输入银行预留手机(必填)", @"请输入验证码(必填)", nil];
    }
    return _placeHoldArray;
}

-(void)configerFooterView
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"添加" forState:UIControlStateNormal];
    [btn setBackgroundColor:ZKcolor(182, 27, 36)];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, screenHeight - 49 , screenWidth, 49);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)confirm
{
    
    
   
    
    if (self.bankcode.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请选择银行"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (self.bankNumber.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入银行卡号"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
   
    if (self.telHp.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入银行预留手机号"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (self.otp.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
    
    
    [SVProgressHUD show];
    NSMutableDictionary *dictOne = [NSMutableDictionary dictionary];
    dictOne[@"accountNumber"] = kAcount;
    dictOne[@"bankCode"] = self.bankcode;
    dictOne[@"bankNumber"] = self.bankNumber;
    dictOne[@"telephoneNumber"] = self.telHp;
    dictOne[@"otp"] = self.otp;
    dictOne[@"token"] = self.token;
    NSString *jsonString = [self dictionaryToJson:dictOne];
    
    NSDictionary *paramet = @{
                              KparametTwo:@"/public/investor/add-bank-otp",
                              KparametOne:jsonString
                              };
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:nil success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showInfoWithStatus:@"添加成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellid";
    HDCFOpenAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [HDCFOpenAccountCell HDCFOpenAccountCell];
    }
    NSString *imgName = self.imageAry[indexPath.row];
    NSString *placeH = self.placeHoldArray[indexPath.row];
    
    cell.textTf.placeholder = placeH;
    cell.textTf.returnKeyType=UIReturnKeyDone;
    cell.textTf.delegate = self;
    cell.textTf.tag = 10 + indexPath.row;
    cell.imgView.image = [UIImage imageNamed:imgName];
    
    [cell.textTf addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
    if (indexPath.row == 3) {
        [cell.sendCodeBtn setBackgroundImage:[UIImage imageNamed:@"button_yanzhengma"] forState:UIControlStateNormal];
        [cell.sendCodeBtn addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
    }else{
        cell.btnWidth.constant = 0;
    }
    if (indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)textChange:(UITextField *)textField{
    if (textField.tag == 11){
        self.bankNumber = textField.text;
    }else if (textField.tag == 12){
        self.telHp = textField.text;
    }else if (textField.tag == 13){
        self.otp = textField.text;
    }
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField.tag == 10) {
        HDCFBankListVc *vc = [[HDCFBankListVc alloc] init];
        vc.delegate = self;
        vc.textField = textField;
        [self.navigationController pushViewController:vc animated:YES];
        return NO;
    }else{
        return YES;
    }
}
- (void)returnWithBankDict:(NSDictionary *)dict withTextField:(UITextField *)textField{
    textField.text = dict[@"bankName"];
    self.bankcode = dict[@"bankCode"];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)sendCode{
   
   
    if (self.bankcode.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请选择银行"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (self.bankNumber.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入银行卡号"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
  
  
    
    if (self.telHp.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入银行预留手机号"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
    [SVProgressHUD show];
    NSMutableDictionary *dictOne = [NSMutableDictionary dictionary];
    dictOne[@"bankCode"] = self.bankcode;
    dictOne[@"accountNumber"] = kAcount;
    dictOne[@"bankNumber"] = self.bankNumber;
    dictOne[@"telephoneNumber"] = self.telHp;
    
    NSString *jsonString = [self dictionaryToJson:dictOne];
    NSDictionary *paramet = @{
                              KparametTwo:@"/public/investor/add-bank-send-otp",
                              KparametOne:jsonString
                              };
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:nil success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showSuccessWithStatus:@"发送成功"];
            [SVProgressHUD  dismissWithDelay:1.0];
            
            self.token = result[@"data"];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD  dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD  dismissWithDelay:1.0];
    }];
}
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    //    NSString *jsonTemp2 = [jsonTemp stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    
    return jsonTemp;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
